<?php
if(!defined('IN_BSTO')){ die('[ERROR] You cannot load this page directly !!!'); }
/**
 * Router class
 *
 * @package    application
 *
 * @author
 * @copyright  (c) pyco24
 * @since      07.2011
 *
 * @version    1.0
 */

class Router
{
	private $registry;

	private $path;

	private $args = array();

	private $arg = null;
    private $arg2 = null;

	public $file;

	public $controller;

	public $action;

	function __construct($registry)
	{
		$this->registry = $registry;
	}

	public function getRoot() {
		$host = 'http://'.$_SERVER['HTTP_HOST'];
		$directory = dirname($_SERVER['SCRIPT_NAME']);
		$root = $directory == '\\' ? $host.'/' : $host.$directory.'/';		
		return $root;
	}

	/**
	 * set controller dir path
	 */
	public function setPath($path)
	{
		// check path
		if ( is_dir($path) == false )
		{
			throw new Exception ('Invalid controller path: "' . $path . '"');
		}
		// set path
		$this->path = $path;
	}


	/**
	 * load the controller
	 */
	public function loader()
	{	
		if ($this->registry->admincp) {	
			$user = $this->registry->db->get('userManager')->getCurrentUser();
		} else {
			$user = NULL;
		}
		// get controller
		$this->getController($user);

		// check file
		if (is_readable($this->file) == false){
			$this->file = $this->path.'/errorController.php';
			$this->controller = 'error';
		}

		// include the controller
		include $this->file;
		
		// create a new controller object
		$class = $this->controller . 'Controller';
		$controller = new $class($this->registry);

		// check if the action is callable
		if ( is_callable(array($controller, $this->action)) == false ){
			$action = 'index';
		}
		else {
			$action = $this->action;
		}
			
		// run the controller
		if ($this->registry->admincp) {
			if ( isset($this->arg2) && ($this->arg2 != null) ) {
				$controller->$action($user, $this->arg, $this->arg2);
			} 
			else if( isset($this->arg) && ($this->arg != null)) {
				//echo "cont = " . $this->controller . " act = " . $this->action . " arg =" . $this->arg;
				$controller->$action($user, $this->arg);
			} 
			else {				
				$controller->$action($user);
                
			}
		}
		else {
			if ( isset($this->arg2) && ($this->arg2 != null) ) {
				$controller->$action($this->arg, $this->arg2);
			}
			else if ( isset($this->arg) && ($this->arg != null) ) {
				$args = explode('-', $this->arg);// edited by hung.vjnh
				//var_dump($args);
				$controller->$action($args[0]);
			} 
			else {
				$controller->$action();
			}
		}
	}


	/**
	 * get the controller
	 */
	private function getController($user)
	{
		//$user = $this->registry->db->get('userManager')->getCurrentUser();
		if($user === null && $this->registry->admincp){ // check login
			$this->controller = 'login';
			$this->action = 'index';							
		}
		else {
			$route = (empty($_GET['act'])) ? '' : $_GET['act'];
				
			if ( empty($route) ){
				$route = 'index';
			}
			else {
					
				// explode the URL parameters
				$parts = explode('/', $route);
				$this->controller = $parts[0];
                
                if(count($parts) == 1){
                    $this->arg = null;
                    $this->arg2 = null;
                    $this->action = 'index';
                }
				else if (count($parts) ==  2){
					$this->arg = $parts[1];
				}
				else {
					//print_r($parts);
					if($this->registry->admincp){						
						if (isset($parts[1])){
							$this->action = $parts[1];
						}
												
						if (isset($parts[2])){
							$this->arg = $parts[2];
						}

						if (isset($parts[3])) {
							$this->arg2 = $parts[3];
						}
					}
					else {
						if (isset($parts[1])){
							$this->arg = $parts[1];
						}
						if (isset($parts[2])){
							$this->arg2 = $parts[2];
						}
					}
				}
			}

			if ( empty($this->controller) ){
				$this->controller = 'index';
			}

			if ( empty($this->action) ){
				$this->action = 'index';
			}


			if($this->registry->admincp){
				//$user = $this->registry->db->get('userManager')->getCurrentUser();
				
				$userType = $user->get('type');
				if(($userType == 'Normal' && !in_array($this->controller, array('book', 'login', 'logout', 'index')))
				|| ($userType != 'Normal' && $userType != 'Admin')){
					$this->controller = 'error';
					$this->action = 'index';
				}
			}
		}

		// set the controller path
		$this->file = $this->path .'/'. $this->controller . 'Controller.php';
		//echo $this->path;
	}
}

?>